import EventRoute from '../EventRoute'
import { desktopCapturer } from 'electron'

const routers: EventRoute[] = []

// 开始拖拽
routers.push(
  new EventRoute('screen-recorder-window-start-drag', (api) => {
    api.screenRecorderWindow.startDrag()
  })
)
// 移动拖拽
routers.push(
  new EventRoute('screen-recorder-window-move-drag', (api) => {
    api.screenRecorderWindow.moveDrag()
  })
)
// 结束拖拽
routers.push(
  new EventRoute('screen-recorder-window-stop-drag', (api) => {
    api.screenRecorderWindow.stopDrag()
  })
)

// 关闭屏幕录制窗口
routers.push(
  new EventRoute('destroy-screen-recorder', (api) => {
    // 先隐藏在销毁
    api.screenRecorderWindow.hide()
    api.screenRecorderWindow.destroy()
    // 显示主窗口
    api.aiIconWindow.show()
  })
)

// 设置窗口大小
routers.push(
  new EventRoute('set-bounds-screen-recorder', async (api, { data }) => {
    const { width, height, isRecording } = data
    const display = api.screenRecorderWindow.getDisplayByCursor()
    let x = display.workArea.x + Math.floor((display.workArea.width - width) / 2)
    let y = display.workArea.y + Math.floor((display.workArea.height - height) / 2)
    if (isRecording) {
      api.screenRecorderWindow.setRecordingWindow()
      y = 0
    } else {
      api.screenRecorderWindow.setEndWindow()
    }
    api.screenRecorderWindow.setBounds( x, y, width, height)
  })
)

// 打开屏幕录制窗口
routers.push(
  new EventRoute('open-screen-recorder', async (api) => {
    api.screenRecorderWindow.create()
  })
)

// 开始录制
routers.push(
  new EventRoute('start-recording', async (api) => {
    api.screenRecorderWindow.startRecording()
  })
)

// 保存视频
routers.push(
  new EventRoute('save-video', async (api) => {
    return api.screenRecorderWindow.saveVideo()
  })
)

// 发送视频数据
routers.push(
  new EventRoute('send-video-data', async (api, { data }) => {
    return api.screenRecorderWindow.sendVideoData(data)
  })
)

// 获取屏幕源
routers.push(
  new EventRoute('get-screen-sources', async (_api, { data }) => {
    return await desktopCapturer.getSources(data)
  })
)

export default routers
